package com.team24.hotel.controller;

import com.team24.hotel.domain.Breakfast;
import com.team24.hotel.domain.BreakfastImage;
import com.team24.hotel.domain.Hotel;
import com.team24.hotel.service.BreakfastImageService;
import com.team24.hotel.service.BreakfastService;
import com.team24.hotel.service.HotelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/breakfast")
public class BreakfastController {
    @Autowired
    private BreakfastService breakfastService;
    @Autowired
    private HotelService hotelService;
    @Autowired
    private BreakfastImageService breakfastImageService;
    @GetMapping("/list")
    public String listBreakfast(@RequestParam("id")int id, Model model){
        List<Breakfast> breakfast = breakfastService.listBreakfast(id);
        Hotel hotel=hotelService.findHotelByPrimaryKey(id);
        model.addAttribute("breakfast",breakfast);
        model.addAttribute("hotel",hotel);
        return "breakfastList";
    }
    @GetMapping("/add")
    public String findBreakfastOfHotel(@RequestParam("id")int id,Model model){
        Hotel hotel=hotelService.findHotelByPrimaryKey(id);
        model.addAttribute("hotel",hotel);
        return "breakfastAdd";
    }
    @PostMapping("/add")
    public  String saveBreakfast(Breakfast breakfast, @RequestParam("file")MultipartFile []file) throws FileNotFoundException {
        int brk=breakfastService.insertBrk(breakfast);
        int hotelId=breakfast.getHotelId();
        String path= ResourceUtils.getURL("").getPath()+"src/main/resources/static/img/";
        path=path.substring(1);
        System.out.println(path);
        int len=file.length;
        for (int i=0;i<len;i++){
            String fileName=file[i].getOriginalFilename();
            String suffixName=fileName.substring(fileName.lastIndexOf("."));
            fileName= UUID.randomUUID()+suffixName;
            File dest=new File(path+fileName);
            if(!dest.getParentFile().exists()){
                dest.getParentFile().mkdirs();
            }    try {
                file[i].transferTo(dest);
            } catch (IOException e) {
                e.printStackTrace();
            }
            BreakfastImage breakfastImage=new BreakfastImage();
            breakfastImage.setBreakfastId(breakfast.getBreakfastId());
            breakfastImage.setImageName(fileName);
            breakfastImageService.saveBreakfastImage(breakfastImage);
        }

        if(brk>0){
            return "redirect:/breakfast/list?id="+hotelId;
        }else {
            return "redirect:error";
        }
    }
    @RequestMapping("/addImage")
    public String addBreakfastImage(@RequestParam("file")MultipartFile []file,@RequestParam("breakfastId")int breakfastId) throws FileNotFoundException {
        String path = ResourceUtils.getURL("src/main/resources/static/img/").getPath();
        path = path.substring(1);
        System.out.println(path);
        int len = file.length;
        for (int i = 0; i < len; i++) {
            String fileName = file[i].getOriginalFilename();
            String suffixName = fileName.substring(fileName.lastIndexOf("."));
            fileName = UUID.randomUUID() + suffixName;
            File dest = new File(path + fileName);
            if (!dest.getParentFile().exists()) {
                dest.getParentFile().mkdirs();
            }
            try {
                file[i].transferTo(dest);
            } catch (IOException e) {
                e.printStackTrace();
            }
            BreakfastImage breakfastImage=new BreakfastImage();
            breakfastImage.setImageName(fileName);
            breakfastImage.setBreakfastId(breakfastId);
            breakfastImageService.saveBreakfastImage(breakfastImage);
        }
        return "redirect:/breakfast/edit?id="+breakfastId;
    }
    @GetMapping("/delete")
    public String deleteBreakfast(@RequestParam("id")int id) throws FileNotFoundException {
        String path= ResourceUtils.getURL("").getPath()+"src/main/resources/static/img/";
        List<BreakfastImage>breakfastImages=breakfastImageService.listBreakfastImage(id);
        for (BreakfastImage breakfastImage:breakfastImages
        ) {
            File file=new File(path+breakfastImage.getImageName());
            if (file.exists()) file.delete();
        }
        Breakfast breakfast=breakfastService.findBrkOfHotel(id);
        int deleted=breakfastService.deleteBrkByPrimaryKey(id);
        int hotelId=breakfast.getHotelId();
        if(deleted>0){
            return "redirect:/breakfast/list?id="+hotelId;
        }else {
            return "redirect:error";
        }
    }
    @RequestMapping("deleteImage")
    @ResponseBody
    public  Boolean deleteImage(int[] imageId) throws FileNotFoundException {
        String path= ResourceUtils.getURL("src/main/resources/static/img/").getPath();
        for (int i=0;i<imageId.length;i++){
            BreakfastImage breakfastImage=breakfastImageService.findBreakfastImage(imageId[i]);
            File file=new File(path+breakfastImage.getImageName());
            if (file.exists()) file.delete();
            breakfastImageService.deleteBreakfastImage(imageId[i]);
        }
        return true;
    }
    @GetMapping("/edit")
    public String updateBrk(@RequestParam("id")int id,Model model){
        List<BreakfastImage> breakfastImages=breakfastImageService.listBreakfastImage(id);
        Breakfast breakfast=breakfastService.findBrkOfHotel(id);
        model.addAttribute("breakfast",breakfast);
        model.addAttribute("breakfastImages",breakfastImages);
        return "breakfastUpdate";
    }
    @PostMapping("/edit")
    public String saveUpdateBrk(Breakfast breakfast){
        int updated=breakfastService.updateBrk(breakfast);
        int hotelId=breakfast.getHotelId();
        if(updated>0){
            return "redirect:/breakfast/list?id="+hotelId;
        }else {
            return "redirect:error";
        }
    }
}
